class ExtpluginController < ApplicationController
layout "admin"
before_filter :login_required
before_filter :initvars
before_filter :init_extplugin
before_filter :matchplugin

  def index
    list
    render :action => 'list'
  end

  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @extplugins =Extplugin.find(:all, :order=>'`extplugins`.`order` ASC')
  end

  def show
    @extplugin = Extplugin.find(params[:id])
  end

  def new
    redirect_to :action=>:list
  end

  def create
    @extplugin = Extplugin.new(params[:extplugin])
		@extplugin.config=Hash.new
    if @extplugin.save
      flash[:notice] = 'Plugin was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
    matchplugin
  end

  def edit
    @extplugin = Extplugin.find(params[:id])
  end

  def update
    @extplugin = Extplugin.find(params[:id])
    if @extplugin.update_attributes(params[:extplugin])
      flash[:notice] = 'Plugin was successfully updated.'
      expire_fragment(@extplugin.name)
      redirect_to :action => 'list'
    else
      render :action => 'edit'
    end
    matchplugin
  end

  def destroy
    Extplugin.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def config
    @extplugin=Extplugin.find(params[:id])
  end
	
  def updatedatas
    @extplugin = Extplugin.find(params[:id].to_i)
    @extplugin.config=params[:extplugin]
    if @extplugin.save
      flash[:notice] = 'Plugin was successfully updated.'
      expire_fragment(@extplugin.name)
      redirect_to :action=> "list"
    else
      render :action => 'edit',:id=>@extplugin.id
    end
  end

  def order
    i=0
    for item in params[:list]
    a=Extplugin.find(item.to_i)
    a.order=i
    a.save
    i+=1
    end
    render :partial=>"list"
  end
  
  def toggle_activate
    @extplugin = Extplugin.find(params[:id])
    if @extplugin.activated==1 
      @extplugin.activated=0 
    else
      @extplugin.activated=1
    end
    @extplugin.save
    expire_fragment(@extplugin.name)
    redirect_to :action => 'list'
  end

end
